import numpy as np
import plotly.express as px

# 定义一个简单的Die类
class Die:
    def __init__(self, num_sides=6):
        self.num_sides = num_sides

    def roll(self):
        return np.random.randint(1, self.num_sides + 1)

# 使用Numpy来实现掷一个骰子
num_sides = 6

# 掷一个骰子1000次，并将结果存储在NumPy数组中
results_single = np.random.randint(1, num_sides + 1, size=1000)

# 分析结果
poss_results_single = np.arange(1, num_sides + 1)
frequencies_single = np.bincount(results_single, minlength=num_sides+1)[1:]

# 可视化结果
title_single = "Results of Rolling One D6 1,000 Times"
labels_single = {'x': 'Result', 'y': 'Frequency of Result'}
fig_single = px.bar(x=poss_results_single, y=frequencies_single, title=title_single, labels=labels_single)
fig_single.show()

# 同时投掷两个骰子
die_1 = Die()
die_2 = Die()

# 掷两个骰子1000次，并将结果存储在列表中
results_double = []
for roll_num in range(1000):
    result = die_1.roll() + die_2.roll()
    results_double.append(result)

# 分析结果
frequencies_double = []
max_result = die_1.num_sides + die_2.num_sides
poss_results_double = range(2, max_result+1)
for value in poss_results_double:
    frequency = results_double.count(value)
    frequencies_double.append(frequency)

# 可视化结果
title_double = "Results of Rolling Two D6 Dice 1,000 Times"
labels_double = {'x': 'Result', 'y': 'Frequency of Result'}
fig_double = px.bar(x=poss_results_double, y=frequencies_double, title=title_double, labels=labels_double)

# 进一步自定义图表
fig_double.update_layout(xaxis_dtick=1)
fig_double.show()